﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;
using BLL;
using Roles;

namespace ERP.Modules
{
    public partial class Ethnic : System.Web.UI.UserControl
    {
        private Ethnic_BLL clsEthnic = new Ethnic_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetData();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //-------Store-Datasource-----------------
        protected void StoreEthnic_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetData();
        }
        //--------Buttom-Main------------------------
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            this.WindowsEthnic.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            CheckboxSelectionModel sm = this.grEthnic.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                ClearTextBox();
                DataDetail(int.Parse(row.RecordID.ToString()));
                this.WindowsEthnic.Show();
                sm.UpdateSelection();
            }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            CheckboxSelectionModel sm = this.grEthnic.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                try
                {
                    Ethnic_BLL.Deletetb_Ethnic(int.Parse(row.RecordID.ToString()));
                }
                catch (Exception)
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                }
            }
            GetData();
        }
        //--------Buttom-Popup------------------------
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtEthnic_ID.Text == string.Empty)
            {
                InsertEthnic();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateEthnic();
                }
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            this.WindowsEthnic.Close();
        }
        //----------Click-------------------
        protected void EthnicDblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int Ethnic_ID = int.Parse(e.ExtraParams["Ethnic_ID"]);
            DataDetail(Ethnic_ID);
            this.WindowsEthnic.Show();
        }
        //----------Button-Windows---------------
        #region[Method]
        protected void GetData()
        {
            DataTable dtEthnic = clsEthnic.BindGirdtb_Ethnic(string.Empty);
            if (dtEthnic != null && dtEthnic.Rows.Count > 0)
            {
                this.StoreEthnic.DataSource = dtEthnic;
                this.StoreEthnic.DataBind();
            }
        }
        protected void DataDetail(int id)
        {
            DataTable dtEthnic = clsEthnic.BindGirdtb_EthnicDetail(id);
            if (dtEthnic != null && dtEthnic.Rows.Count > 0)
            {
                txtEthnic_ID.Text = dtEthnic.Rows[0]["Ethnic_ID"].ToString();
                txtEthnic_Alias.Text = dtEthnic.Rows[0]["Ethnic_Alias"].ToString();
                txtEthnic_Name.Text = dtEthnic.Rows[0]["Ethnic_Name"].ToString();
                txtEthnic_Note.Text = dtEthnic.Rows[0]["Ethnic_Note"].ToString();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsEthnic = new Ethnic_BLL();
            var dtEthnic = clsEthnic.BindGirdtb_Ethnic(string.Empty);
            if (dtEthnic != null && dtEthnic.Rows.Count > 0)
            {
                foreach (DataRow dr in dtEthnic.Rows)
                {
                    if (txtEthnic_ID.Text != dr["Ethnic_ID"].ToString())
                    {
                        if (txtEthnic_Alias.Text == dr["Ethnic_Alias"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã dân tộc đã tồn tại !");
                        }
                        else if (txtEthnic_Name.Text == dr["Ethnic_Name"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tên dân tộc đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertEthnic()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Ethnic_ET
                    {
                        Ethnic_Alias = txtEthnic_Alias.Text,
                        Ethnic_Name = txtEthnic_Name.Text,
                        Ethnic_Note = txtEthnic_Note.Text,
                        Ethnic_Date_Add = DateTime.Now,
                        Ethnic_Computer_Add = Environment.MachineName
                    };
                    Ethnic_BLL.Inserttb_Ethnic(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        protected void UpdateEthnic()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Ethnic_ET
                    {
                        Ethnic_ID = int.Parse(txtEthnic_ID.Text),
                        Ethnic_Alias = txtEthnic_Alias.Text,
                        Ethnic_Name = txtEthnic_Name.Text,
                        Ethnic_Note = txtEthnic_Note.Text,
                        Ethnic_Date_Modify = DateTime.Now,
                        Ethnic_Computer_Modify = Environment.MachineName
                    };
                    Ethnic_BLL.Updatetb_Ethnic(obj);
                    this.WindowsEthnic.Close();
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Sữa thành công!");
                    GetData();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtEthnic_ID.Text = string.Empty;
            txtEthnic_Alias.Text = MaTuDong();
            txtEthnic_Name.Text = string.Empty;
            txtEthnic_Note.Text = string.Empty;
        }
        #endregion
        #region[MaTangTuDong]
        protected string MaTuDong()
        {
            int i = 0;
            DataTable dt = clsEthnic.BindGirdtb_Ethnic(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Ethnic_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Ethnic_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Ethnic_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "DT_00" + i;
        }
        #endregion
        #region[Search]
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtEthnic = clsEthnic.BindGirdtb_Ethnic(txtSearch.Text);
            if (dtEthnic != null && dtEthnic.Rows.Count > 0)
            {
                this.StoreEthnic.DataSource = dtEthnic;
                this.StoreEthnic.DataBind();
            }
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Ethnic, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelEthnic.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Ethnic, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}